//
//  CoverImageView.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/6.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

@IBDesignable class CoverImageView: UIImageView {

  // MARK: - Private vars
  
  private var shadowLayer: CALayer?
  private var newLayer: CALayer?
  
  // MARK: - Private methods
  
  internal func setup() {
    drawShadow()
  }
  
  private func drawShadow() {
    shadowLayer?.removeFromSuperlayer()
    shadowLayer = CALayer()
    shadowLayer!.frame = bounds
    shadowLayer!.backgroundColor = UIColor(fromRGB: 0x424242).cgColor
    
    shadowLayer!.masksToBounds = false
    shadowLayer!.shadowColor = UIColor.black.cgColor
    shadowLayer!.shadowOpacity = shadowAlpha
    shadowLayer!.shadowOffset = CGSize(width: 0, height: 0)
    shadowLayer!.shadowRadius = shadowRadius
    shadowLayer!.shouldRasterize = true
    layer.insertSublayer(shadowLayer!, at: 0)
    
    layer.borderColor = strokeColor.cgColor
    layer.borderWidth = 1
    
    newLayer?.removeFromSuperlayer()
    newLayer = CALayer()
    newLayer!.frame = bounds
    newLayer!.contents = layer.contents
    layer.addSublayer(newLayer!)
  }
  
  // MARK: - Public vars
  
  @IBInspectable var shadowRadius: CGFloat = 8 {
    didSet {
      setup()
    }
  }
  
  @IBInspectable var shadowAlpha: Float = 0.6 {
    didSet {
      setup()
    }
  }
  
  @IBInspectable var strokeColor: UIColor = UIColor.white {
    didSet {
      setup()
    }
  }

  
  // MARK: - Lifecycle
  
  override var image: UIImage? {
    didSet {
      setup()
    }
  }
  
  override var frame: CGRect {
    didSet {
      setup()
    }
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    setup()
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  
}
